package com.ruan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruan.entity.Comments;
import com.ruan.entity.Result;
import com.ruan.query.CommentsQuery;
import com.ruan.service.CommentsService;
import com.ruan.utils.OssUtil;
import com.ruan.vo.CommentsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName:CommentsController
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/7/4 11:55
 **/
@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @PostMapping("/uploadCommentsPicture")
    public Result uploadCommentsPicture(MultipartFile file) throws IOException {
        //获取上传的文件名
        String fileName = file.getOriginalFilename();

        //产生新的文件名，时间戳+原先文件名后缀
        String newFileName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf('.'));

        String path = "commentsPicture/";

        String avatar = OssUtil.upload(newFileName, file.getInputStream(),path);

        return new Result(200,avatar);
    }

    @PostMapping("/addComments")
    public Result addComments(Comments comments){
        return commentsService.addComments(comments);
    }

    @PostMapping("/getCommentsList")
    public Result getCommentsList(CommentsQuery commentsQuery){
        //开启分页
        PageHelper.startPage(commentsQuery.getPageNum(),commentsQuery.getPageSize());

        List<CommentsVo> commentsList = commentsService.getCommentsList(commentsQuery);

        //当前页，下一页，上一页，首页，尾页。。。。需要一个类进行封装，
        PageInfo<CommentsVo> commentsPageInfo = new PageInfo<>(commentsList);

        return new Result(200,commentsPageInfo);
    }

    @RequestMapping("/deleteComments/{id}")
    public Result deleteComments(@PathVariable Integer id){
        return commentsService.deleteComments(id);
    }

    @PostMapping("/deleteCommentsByIds")
    public Result deleteCommentsByIds(@RequestParam("ids[]") Integer[] ids){
        return commentsService.deleteCommentsByIds(ids);
    }
}
